// 直线带箭头
function drawFlow(svg, elementId, strokeColor, positions) {
    $.each(positions, function(i, m){
        if (0 < i) {
            var hasArrow = false;
            var x1 = positions[i-1].x;
            var y1 = positions[i-1].y;
            if (i == positions.length - 1) {
                hasArrow = true;
            }
            drawLine(svg, elementId, strokeColor, x1, y1, m.x, m.y, hasArrow)
        };
    });
};

// 网关
function drawGateway(svg, elementId, strokeColor, gatex, gatey, gateWidth, gateHeight, elementType, pathTitle) {
    // 找出中心点
    var x = gatex + gateWidth/2;
    var y = gatey + gateHeight/2;
    // 先画一个菱形
    drawDiamond(svg, elementId, strokeColor, x, y, gateWidth, gateHeight, pathTitle);
    // 再根据类型画其他图形
    var strokeWidth = 4;
    var points = "";
    if (elementType == "exclusiveGateway") {
        var xp = 4*gateWidth/20;
        var yp = 4*gateHeight/20;
        points = (x-xp) + "," + (y-yp) +" " + (x+xp) + "," + (y+yp);
        drawPolyLine(svg, elementId, strokeColor, points, strokeWidth, pathTitle);
        points = "";
        points = (x+xp) + "," + (y-yp) +" " + (x-xp) + "," + (y+yp);
        drawPolyLine(svg, elementId, strokeColor, points, strokeWidth, pathTitle);
    } else if (elementType == "inclusiveGateway") {
        drawEclipse(svg, elementId, strokeColor, "white", x, y, gateWidth/4, gateHeight/4, 3, "");
    } else if (elementType == "parallelGateway") {
        var xc = gateWidth/4;
        var yc = gateHeight/4;
        points = "";
        points = (x-xc) + "," + y +" " + (x+xc) + "," + y;
        drawPolyLine(svg, elementId, strokeColor, points, strokeWidth, pathTitle);
        points = "";
        points = x + "," + (y-yc) +" " + x + "," + (y+yc);
        drawPolyLine(svg, elementId, strokeColor, points, strokeWidth, pathTitle);
    };
};

// 画菱形 x、y为中心点坐标
function drawDiamond(svg, elementId, strokeColor, diamondx, diamondy, diamondWidth, diamondHeight, pathTitle) {
    var points = "";
    points += (diamondx-diamondWidth/2) + "," + diamondy + " ";
    points += diamondx + "," + (diamondy-diamondHeight/2) + " ";
    points += (diamondx+diamondWidth/2) + "," + diamondy + " ";
    points += diamondx + "," + (diamondy+diamondHeight/2) + " ";
    points += (diamondx-diamondWidth/2) + "," + diamondy;
    drawPolyLine(svg, elementId, strokeColor, points, 1, pathTitle);
};

// 画三角形，x、y为圆点坐标
function drawTriangle(svg, elementId, strokeColor, x, y, cr) {
    var points = "";
    points += (x + cr) + "," + y + " ";
    points += (x + cr*1/5) + "," + (y+cr*39/25) + " ";
    points += (x + cr*9/5) + "," + (y+cr*39/25) + " ";
    points += (x + cr) + "," + y + " ";
    drawPolyLine(svg, elementId, strokeColor, points, 1);
};

// 画计时器
function drawTimer(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x)
        .attr("y", y)
        .attr("width", width)
        .attr("height", height)
        .attr("viewBox", "0 0 32 32")
        .append("g")
        // .attr("pointer-events","fill")
        .each(function (d) {
            drawCircle(d3.select(this), elementId, strokeColor, "white", 16, 16, 15, 1, dasharray, "");
            drawCircle(d3.select(this), elementId, strokeColor, "none", 16, 16, 15, 1, dasharray, "");
            drawCircle(d3.select(this), elementId, strokeColor, "none", 16, 16, 10, 1, dasharray, "");
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M 16 6 L 16 9 M 21 7 L 19.5 10 M 25 11 L 22 12.5 M 26 16 L 23 16 M 25 21 L 22 19.5 M 21 25 L 19.5 22 M 16 26 L 16 23 M 11 25 L 12.5 22 M 7 21 L 10 19.5 M 6 16 L 9 16 M 7 11 L 10 12.5 M 11 7 L 12.5 10 M 18 9 L 16 16 L 20 16");
        });
};

// 信息边界事件
function drawSignal(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x)
        .attr("y", y)
        .attr("width", width)
        .attr("height", height)
        .attr("viewBox", "0 0 32 32")
        .append("g")
        .attr("pointer-events","fill")
        .each(function (d) {
            drawCircle(d3.select(this), elementId, strokeColor, "white", 16, 16, 15, 1, dasharray, "");
            drawCircle(d3.select(this), elementId, strokeColor, "none", 16, 16, 15, 1, dasharray, "");
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M 8.7124971,21.247342 L 23.333334,21.247342 L 16.022915,8.5759512 L 8.7124971,21.247342 z");
        });
};

// 错误图标
function drawError(svg, elementId, strokeColor, x, y, width, height, size, dasharray, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x)
        .attr("y", y)
        .attr("width", width)
        .attr("height", height)
        .attr("viewBox", "0 0 32 32")
        .append("g")
        .attr("pointer-events","fill")
        .each(function (d) {
            drawCircle(d3.select(this), elementId, strokeColor, "white", 16, 16, 15, 1, dasharray, "");
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M 22.820839,11.171502 L 19.36734,24.58992 L 13.54138,14.281819 L 9.3386512,20.071607 L 13.048949,6.8323057 L 18.996148,16.132659 L 22.820839,11.171502 z");
        });
};

// 画用户图标
function drawUserTask(svg, elementId, x, y, width, height, strokeColor, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x + 5)
        .attr("y", y + 2)
        .attr("width", "15px")
        .attr("height", "15px")
        .attr("viewBox", "0 0 60 60")
        .append("g")
        .each(function (d) {
            drawPath(d3.select(this), elementId, strokeColor, "white", 1, "M7.757,58.934h51.667V41.758c0,0-5.114-8.586-15.527-11.328H24.751C12.313,33.384,7.575,43.037,7.575,43.037L7.757,58.934z");
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M18.49,49.799v8.771");
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M48.051,49.799v8.771");
            drawCircle(d3.select(this), elementId, strokeColor, "#010101", 33, 18, 12, 1, "", "");
            drawPath(d3.select(this), elementId, strokeColor, "#F1F0F1", 1, "M22.972,21.474c0,0,6.17-5.319,11.788-3.962C40.379,18.87,44.9,15.994,44.9,15.994c0.549,3.701,0.093,8.222-3.199,12.243c0,0,2.377,1.644,2.377,3.289c0,1.647,0.272,4.111-2.193,6.58c-2.465,2.465-12.061,2.741-14.801,0c-2.741-2.742-2.741-4.02-2.741-5.849c0-1.828,1.278-2.74,2.741-4.202C24.708,26.775,21.604,22.845,22.972,21.474z");
        });
};

// 齿轮
function drawServiceTask(svg, elementId, x, y, width, height, strokeColor, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x)
        .attr("y", y - 4)
        .attr("width", "60px")
        .attr("height", "60px")
        .attr("viewBox", "0 0 60 60")
        .append("g")
        .attr("transform","scale(0.7,0.7) translate(8,8)")
        .each(function (d) {
            drawPolygon(d3.select(this), elementId, strokeColor, "#ffffff", 1, "15.392,5.064 17.954,2.502 20.347,4.895 17.786,7.455 18.729,9.732 22.353,9.732 22.353,13.115 18.731,13.115 17.788,15.392 20.351,17.955 17.958,20.347 15.397,17.786 13.12,18.729 13.12,22.353 9.737,22.353 9.737,18.731 7.46,17.788 4.897,20.35 2.506,17.958 5.066,15.397 4.124,13.12 0.5,13.12 0.5,9.737 4.121,9.737 5.065,7.461 2.503,4.898 4.895,2.506 7.455,5.066 9.732,4.125 9.732,0.5 13.116,0.5 13.116,4.121")
            drawCircle(d3.select(this), elementId, strokeColor, "none", 11.427, 11.426, 3.714, 1, "", "");
        });
};

// 信封
function drawReceiveTask(svg, elementId, x, y, width, height, strokeColor, pathTitle) {
    svg.append("svg")
        .attr("name", elementId)
        .attr("x", x - 2)
        .attr("y", y - 6)
        .attr("width", "60px")
        .attr("height", "60px")
        .attr("viewBox", "0 0 60 60")
        .append("g")
        .each(function (d) {
            drawPath(d3.select(this), elementId, strokeColor, "none", 1, "M8,11 L8,21 L24,21 L24,11z M8,11 L16,17 L24,11");
        });
};

// 
function drawScriptTask(svg, elementId, x, y, width, height, strokeColor, pathTitle) {};